Timing a section of code

I am interested in timing a section of code in order to get execution time. Does anyone have experience in this type of debugging/optimization?
PatrickGuay - Wed May 23 09:40:41 EDT 2012

Re: Timing a section of code
Mathias Mamsch - Wed May 23 11:07:48 EDT 2012

You can look at the prototype for a DXL standard library, that I put on sourceforge a long time ago. Never came around to extend that library, mostly due the lack of help from outside. I think it had a good concept for profiling functions. The first idea is, to make the measurement more robust against the hardware you are running on. For that you need to compare the time that a function takes to a fixed value, e.g. the time for an empty function call. Therfore if a function takes 100 EFC it means it takes 100x longer as an empty function call. Of course this is not exact, but I guess it better than just measuring time.

To measure small execution times you need to call a function multiple times and divide the time it took, by the number of executions. The number of times you need to execute the function however depends on the execution time of the function. You may need to call a string function 10000 times to get a measurable result, while you might need to call a function that performs a complex operation only once. The profiling.inc contains a function that will automatically determine how many times a function needs to be called, until the complete execution time is > 500ms that way you get a more stable result for functions that have very small execution times.

The next thing is averaging results. Depending on the load of the computer you might want to make the measurement multiple times and take the average. That is also built into the profiling.inc. Last but not least, the DXL standard library prototype has an automatization for executing a profiling suite on set of library functions, so you can measure performance changes of library function long term.

All in all it might be worth taking a look: http://sourceforge.net/projects/dxlstandardlib/

You should be able to download and run it, if there are any questions, do not hesitate to ask.

Maybe that helps, regards, Mathias

Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS

Re: Timing a section of code
PatrickGuay - Wed May 23 12:28:03 EDT 2012

Good suggestions, I will have a look at the library

Cheers,

Re: Timing a section of code
llandale - Wed May 23 12:34:40 EDT 2012

I imagine Mathias' approach is significantly precise and robust.

However, this is what I do:

int TimeStart = getTickCount_(), TimeTotal
//for whatever my loop is
//{  Do stuff in the loop
//}
infoBox("Pause")
TimeTotal = (getTickCount_() - TimeStart)
print "Whatever execution time is " TimeTotal  "msecs\n"


-Louie